#load packages

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.0      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(p8105.datasets)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

#filter dataset with variables boro, cuisine_description, inspection_date, score, grade only

data("rest_inspec")
nyc_restaurant = rest_inspec %>% 
  select(boro, cuisine_description, inspection_date, score, grade) %>% 
  filter(.data = ., boro == "MANHATTAN") %>% 
  na.omit()

#make a bar plot with count of different types of restaurant

nyc_restaurant %>% 
  count(cuisine_description) %>% 
  mutate(cuisine_description = fct_reorder(cuisine_description, n)) %>% 
  plot_ly(
    x = ~cuisine_description, y = ~n, color = ~cuisine_description, type = "bar",
    colors = "viridis"
  )

#make a box plot with the score of different types of restaurant

nyc_restaurant %>% 
  mutate(cuisine_description = fct_reorder(cuisine_description, score)) %>% 
  plot_ly(y = ~score, color = ~cuisine_description, type = "box", colors = "viridis")

#make a bar plot with the grade of different types of restaurant

nyc_restaurant %>% 
  group_by(cuisine_description, grade) %>% 
  summarize(count = n()) %>% 
  plot_ly(
    y = ~count, x = ~cuisine_description, color = ~grade, 
    type = "bar", colors = "viridis"
  )
## `summarise()` has grouped output by 'cuisine_description'. You can override
## using the `.groups` argument.

#make a scatter plot with the score of different types of restaurant across years

nyc_restaurant %>% 
  mutate(inspection_date = as.Date(inspection_date)) %>%
  plot_ly(
    x = ~inspection_date, y = ~score, type = "scatter", mode = "markers",
    color = ~cuisine_description, alpha = 0.5
  )
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

#load package flexdashboard

library(flexdashboard)